<?php

namespace app\api\controller;

use app\common\controller\Common;

use think\Request;
use com\verify\JdeVerify;
class Login extends Common
{

    public function login()
    {
        $userModel = model('User');
        $param = $this->param;
        $username = $param['account'] ? $param['account'] :$param['username'];
        $password = $param['password'];
        $verifyCode = !empty($param['verifyCode']) ? $param['verifyCode'] : '';
        $isRemember = !empty($param['isRemember']) ? $param['isRemember'] : '';
        $data = $userModel->login($username, $password, $verifyCode, $isRemember);
        
        if (!$data) {
            return resultArray(['error' => $userModel->getError()]);

        }
        //登录日志
        $request = Request::instance();
        $ruleName = $request->module() . '-' . $request->controller() . '-' . $request->action();
   
        //热词模型生成结束的钩子
        $arg = [
            'uid' => $data['userId'],
            'rule' => $ruleName,
            'comment' => $param,
        ];
        //日志记录
        $logModel = new \app\common\Log(new \app\api\model\Log());
        $logModel->info('log', $arg);

        return resultArray(['data' => $data]);
    }

    public function logout()
    {
        $param = $this->param;
        cache('Auth_' . $param['token'], null);
        return resultArray(['data' => '退出成功']);
    }

    public function getVerify()
    {
        $captcha = new JdeVerify(config('captcha'));
        return $captcha->entry();
    }
}
